package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.IdUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.RandomStringUtils;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.timer.SystemClock;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mp.entity.WxMpTemplateMessageDto;
import org.dromara.common.mp.entity.WxMpTemplateMessageResult;
import org.dromara.common.mp.utils.WxMpUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysUserSocialBo;
import org.dromara.system.domain.vo.SysUserSocialVo;
import org.dromara.system.service.ISysUserSocialService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * 社交关联
 *
 * @author Denny
 * @date 2025-10-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userSocial")
public class SysUserSocialController extends BaseController {

    private final ISysUserSocialService sysUserSocialService;

    /**
     * 发送微信模版消息
     */
    @SaCheckPermission("system:userSocial:list")
    @PostMapping("/sendTemplateMessage")
    public R<WxMpTemplateMessageResult> sendTemplateMessage(@Validated @RequestBody WxMpTemplateMessageDto templateMessage) {
        WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
            .templateId(templateMessage.getTemplateId())
            .toUser(templateMessage.getOpenId()).build();
        String date = DateUtils.formatDateTime(new Date(SystemClock.now()));
        wxMpTemplateMessage
            .addData(new WxMpTemplateData("first", "测试内容", "#FF00FF"))
            .addData(new WxMpTemplateData("k1", date, "#FF00FF"))
            .addData(new WxMpTemplateData("k2", "测试字段", "#FF00FF"));
        WxMpTemplateMessageResult wxMpTemplateMessageResult = WxMpUtils.sendTemplateMessage(wxMpTemplateMessage);
        return R.ok(wxMpTemplateMessageResult);
    }
    /**
     * 查询社交关联列表
     */
    @SaCheckPermission("system:userSocial:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserSocialVo> list(SysUserSocialBo bo, PageQuery pageQuery) {
        return sysUserSocialService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出社交关联列表
     */
    @SaCheckPermission("system:userSocial:export")
    @Log(title = "社交关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserSocialBo bo, HttpServletResponse response) {
        List<SysUserSocialVo> list = sysUserSocialService.queryList(bo);
        ExcelUtil.exportExcel(list, "社交关联", SysUserSocialVo.class, response);
    }

    /**
     * 获取社交关联详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:userSocial:query")
    @GetMapping("/{id}")
    public R<SysUserSocialVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysUserSocialService.queryById(id));
    }

    /**
     * 新增社交关联
     */
    @SaCheckPermission("system:userSocial:add")
    @Log(title = "社交关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserSocialBo bo) {
        return toAjax(sysUserSocialService.insertByBo(bo));
    }

    /**
     * 修改社交关联
     */
    @SaCheckPermission("system:userSocial:edit")
    @Log(title = "社交关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserSocialBo bo) {
        return toAjax(sysUserSocialService.updateByBo(bo));
    }

    /**
     * 删除社交关联
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:userSocial:remove")
    @Log(title = "社交关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUserSocialService.deleteWithValidByIds(List.of(ids), true));
    }
}
